package cn.com.aiouyi.config;

import java.lang.reflect.Method;

import javax.annotation.PostConstruct;
import javax.servlet.MultipartConfigElement;
import javax.servlet.ServletContext;

import org.apache.commons.lang3.StringUtils;
import org.springframework.aop.MethodBeforeAdvice;
import org.springframework.aop.framework.ProxyFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.web.servlet.MultipartConfigFactory;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.web.servlet.config.annotation.InterceptorRegistry;
import org.springframework.web.servlet.config.annotation.ResourceHandlerRegistry;
import org.springframework.web.servlet.config.annotation.WebMvcConfigurer;

import cn.com.aiouyi.common.Setting;
import cn.com.aiouyi.exception.handler.FreeMarkerExceptionHandler;
import cn.com.aiouyi.interceptor.ShareRenderArgsVariableInterceptor;
import cn.com.aiouyi.template.method.MessageMethod;
import cn.com.aiouyi.util.SystemUtils;

@Configuration
public class WebMvcConfig implements WebMvcConfigurer{

	@Autowired
    private freemarker.template.Configuration configuration;
	@Autowired
	private ServletContext servletContext;
	@Autowired
	private MessageMethod messageMethod;
	
    @Override
    public void addResourceHandlers(ResourceHandlerRegistry registry) {
        registry.addResourceHandler("/resource/**").addResourceLocations("/resource/");
    }
    
    @Override
    public void addInterceptors(InterceptorRegistry registry) {
    	registry.addInterceptor(new ShareRenderArgsVariableInterceptor()).addPathPatterns("/**");
    }
    
    // Spring 初始化的时候加载配置
    @PostConstruct
    public void setConfigure() throws Exception {
    	// 代理setting
    	Setting setting = SystemUtils.getSetting();
    	ProxyFactory proxyFactory = new ProxyFactory(setting);
		proxyFactory.setProxyTargetClass(true);
		proxyFactory.addAdvice(new MethodBeforeAdvice() {

			public void before(Method method, Object[] args, Object target) throws Throwable {
				if (StringUtils.startsWith(method.getName(), "set")) {
					throw new UnsupportedOperationException("Operation not supported");
				}
			}

		});
		
        configuration.setSharedVariable("base", servletContext.getContextPath());
        configuration.setSharedVariable("i18n", messageMethod);
        configuration.setSharedVariable("setting", proxyFactory.getProxy());
        configuration.setTemplateExceptionHandler(new FreeMarkerExceptionHandler());
        // 支持中括号
        configuration.setTagSyntax(freemarker.template.Configuration.AUTO_DETECT_TAG_SYNTAX);
    }
    
    /**  
     * 文件上传配置  
     * @return  
     */  
    @Bean  
    public MultipartConfigElement multipartConfigElement() {  
        MultipartConfigFactory factory = new MultipartConfigFactory();  
        //单个文件最大  
        factory.setMaxFileSize("20480KB"); //KB,MB  
        /// 设置总上传数据总大小  
        factory.setMaxRequestSize("20480KB");  
        return factory.createMultipartConfig();  
    } 
}
